/* 
 * @Author: 21克的爱情
 * @Date: 2020-09-14 09:12:19
 * @Email: raohong07@163.com
 * @LastEditors: 21克的爱情
 * @LastEditTime: 2020-09-14 09:40:25
 * @Description: 广告页面
 */
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:pangolin/pangolin.dart' as Pangolin;

class ScreenPage extends StatefulWidget {
  ScreenPage({Key key}) : super(key: key);

  @override
  _ScreenPageState createState() => _ScreenPageState();
}

class _ScreenPageState extends State<ScreenPage> {
  @override
  void initState() {
    Pangolin.pangolinResponseEventHandler.listen((value){
      if(value is Pangolin.onRewardResponse) {
          print("激励视频回调：${value.rewardVerify}");
          print("激励视频回调：${value.rewardName}");
          print("激励视频回调：${value.rewardAmount}");

          if(value.rewardName == "rewardVideo Close") {
            debugPrint("视频关闭了");
          }
        } else {
          print("回调类型不符合");
        }
    });
    super.initState();
    initPlatformState();
  }

  @override
  void dispose() { 
    Pangolin.removeBannerAd();
    super.dispose();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.phone,
      Permission.location,
      Permission.storage,
    ].request();
    //校验权限
    if(statuses[Permission.location] != PermissionStatus.granted){
      print("无位置权限");
    }
    _initPangolin();
    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;
  }

//  "5056758",
//  true,
//  "爱看",
//  true,
//  true,
//  true,
//  true
  _initPangolin() async {
    await Pangolin.registerPangolin(
      appId: "5105337",
      useTextureView: true,
      appName: "爱吾影视",
      allowShowNotify: true,
      allowShowPageWhenScreenLock: true,
      debug: true,
      supportMultiProcess: true
    ).then((v){
      _loadSplashAd();
    });
  }

  _loadSplashAd() async {
    Pangolin.loadSplashAd(
      mCodeId: "887380229",
      debug: true
    ).then((value){
      print('------------------$value');
    }) ;
  }

  //945122969
  _loadRewardAd() async {
    await Pangolin.loadRewardAd(
      isHorizontal: false,
      debug: true,
      mCodeId: "945477676",
      supportDeepLink: true,
      rewardName: "吾币",
      rewardAmount: 20,
      isExpress: true,
      expressViewAcceptedSizeH: 500,
      expressViewAcceptedSizeW: 500,
      userID: "user123",
      mediaExtra: "media_extra"
    );
  }

  _loadBannerAd() async {
    await Pangolin.loadBannerAd(
      mCodeId: "945477670",
      supportDeepLink: false,
      expressViewWidth: 600,
      expressViewHeight: 90,
      isCarousel: true,
      interval: 40,
      topMargin: 280
    );
  }

  _loadInterstitialAd() async {
    await Pangolin.loadInterstitialAd(
      mCodeId: "945477674",
      expressViewWidth: 300,
      expressViewHeight: 300
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              FlatButton(
                onPressed: () {
                  _loadBannerAd();
                },
                child: Text("banner"),
              ),
              FlatButton(
                onPressed: () {
                  Pangolin.removeBannerAd();
                },
                child: Text("removeBanner"),
              ),
            ]
          ),
        ),
      ),
    );
  }
}
